package com.zhengtoudai.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by ZZM on 2016/4/14.
 *
 * 数字转金额相关工具类
 */
public class DecimalFormatUtils {

    /**
     * 将数字转化成银行数字
     * @param s 输入的金额
     * @param len 小数点后面保留几位
     * @return 比如输入2000 转化成2,000.00
     */
    public static String formatMoney(String s, int len)
    {
        if (s == null || s.length() < 1) {
            return "";
        }
        NumberFormat formater = null;
        double num = Double.parseDouble(s);
        if (len == 0) {
            formater = new DecimalFormat("###,###");

        } else {
            StringBuffer buff = new StringBuffer();
            buff.append("###,###.");
            for (int i = 0; i < len; i++) {
                buff.append("#");
            }
            formater = new DecimalFormat(buff.toString());
        }
        String result = formater.format(num);
        if(result.indexOf(".") == -1)
        {
            result = result + ".00";
        }
//        else
//        {
//            result = "￥" + result;
//        }
        return result;
    }
}
